package com.dodoke.cake.service.impl;

import com.dodoke.cake.bean.Classification;
import com.dodoke.cake.dao.CatalogDao;
import com.dodoke.cake.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class CatalogServiceImpl implements CatalogService {
    @Autowired
    private CatalogDao catalogDao;


    @Override
    public List<Classification> getAllData() {
        List<Classification> list1 = new ArrayList<>();
        List<Classification> list2 = new ArrayList<>();
        List<Classification> list3 = new ArrayList<>();
        List<Classification> list = new ArrayList<>();
        List<Classification> catalogs = this.catalogDao.getAllData();
        // 将所有数据按照自己的等级进行归类
        for (Classification cata : catalogs) {
            if(cata.getLevelId() == 1) {
                list1.add(cata);
            } else if(cata.getLevelId() == 2) {
                list2.add(cata);
            } else if(cata.getLevelId() == 3) {
                list3.add(cata);
            }
        }
        // 重新排列数据的顺序
        for(int i=0; i<list1.size(); i++) {
            Classification classification = list1.get(i);
            list.add(classification);
            for(int j=0; j<list2.size(); j++) {
                Classification classification1 = list2.get(j);
                if(classification1.getParentId() == classification.getId()) {
                    list.add(classification1);
                    for(int k=0; k<list3.size(); k++) {
                        Classification classification2 = list3.get(k);
                        if(classification2.getParentId() == classification1.getId()) {
                            list.add(classification2);
                        }
                    }
                }
            }
        }
        return list;
    }

    @Override
    public void deleteCatalog(int id) {
        this.catalogDao.deleteCatalog(id);
    }

    @Override
    public void addCatalog(String catalogName, int levelId, int parentId, String description) {
        this.catalogDao.addCatalog(catalogName, levelId, parentId, description);
    }

    @Override
    public int selectLevelId(int id) {
        return this.catalogDao.selectLevelId(id);
    }

    @Override
    public int getLevelIdByTypeName(String typeName) {
        return this.catalogDao.getLevelIdByTypeName(typeName);
    }

    @Override
    public int getIdByTypeName(String typeName) {
        return this.catalogDao.getIdByTypeName(typeName);
    }

    @Override
    public void addCatelogList(Classification cf) {
        this.catalogDao.addCatelogList(cf);
    }
}
